<?php
function staffId($id) {
	return $id;
}

function mongoRegx($str) {
	$regx = implode('|', mb_split('/\s+/', preg_quote($str)));
	// return new MongoDB\BSON\Regex($regx, 'i');
	return new MongoRegex("/$regx/i");
}
function saltId($id) {
	return ($id << 12) + hexdec(substr(md5($id), 16, 3));
}
function explodeTrim($delimiter, $str) {
	$list = explode($delimiter, $str);
	$res = array();
	foreach ($list as $v) {
		$v = trim($v);
		if ($v) {
			$res[] = $v;
		}
	}
	return $res;
}
function formatTags($str) {
	return explodeTrim(',', $str);
}
function formatLines($str) {
	return explodeTrim("\n", $str);
}


function getInChargeDepartment($id=0) {
	if ($id == 0) {
		$id = fpsUser::id();
	}

	$departments = fps_config('departments');
	$inchrges = [];

	foreach ($departments as $depart) {
		if ($depart['approver'] == $id) {
			$inchrges[] = $depart['id'];
		}
	}

	return $inchrges;
}

function checkAccess($right, $noThrow = false)
{
	$user = fpsUser::get();
	if(in_array($right, $user['acl']))
	{
		return true;
	}
	else if($noThrow)
	{
		return false;
	}

	return json_out(400, '没有权限');
}

function exportGetHeader($fileName)
{
	$fileName = urlencode($fileName);
	header('Content-type: text/csv; charset=utf-8');
	header('Content-Disposition: attachment; filename="'.$fileName.'"');
	header('Cache-Control: must-revalidate,post-check=0,pre-check=0');
	header('Expires: 0');
	header('Pragma: public');
	echo "\xEF\xBB\xBF";
}


// 获取用户的部门和上级审核人信息
function getUserExtraInfo($id){
    // 部门
    $departments = fps_config('departments', []);

    $user = fpsUser::by_id($id);

    $result = ['department_name'=>'', 'approver'=>''];

    if ($user) {

        if (val($user, 'department') && val($departments, $user['department'])) {
            $result['department_name'] = $departments[$user['department']]['name'];

            $approver = fpsUser::by_id($departments[$user['department']]['approver']);
            $result['approver'] = $approver ? $approver['name'] : '';
        }

    }

    return $result;
}

// 转换编码
function encodeToUtf8($string) {
     return mb_convert_encoding($string, "UTF-8", mb_detect_encoding($string, "UTF-8, GBK, ISO-8859-1, ISO-8859-15", true));
}

function encodeToIso($string) {
     return mb_convert_encoding($string, "ISO-8859-1", mb_detect_encoding($string, "UTF-8, GBK, ISO-8859-1, ISO-8859-15", true));
}


// ssl长字符串加密
function ssl_encrypt($source,$type,$key){
	//Assumes 1024 bit key and encrypts in chunks.

	$maxlength= (APP_PRIVATE_BITS >> 3) - 11;
	$output='';
	while($source){
		$input= substr($source,0,$maxlength);
		$source=substr($source,$maxlength);
		if($type=='private'){
			$ok= openssl_private_encrypt($input,$encrypted,$key);
		}else{
			$ok= openssl_public_encrypt($input,$encrypted,$key);
		}
		$output.=$encrypted;
	}
	return $output;
}

// ssl长字符串解密
function ssl_decrypt($source,$type,$key){
	// The raw PHP decryption functions appear to work
	// on 128 Byte chunks. So this decrypts long text
	// encrypted with ssl_encrypt().

	$maxlength=(APP_PRIVATE_BITS >> 3);
	$output='';
	while($source){
		$input= substr($source,0,$maxlength);
		$source=substr($source,$maxlength);
		if($type=='private'){
			$ok= openssl_private_decrypt($input,$out,$key);
		}else{
			$ok= openssl_public_decrypt($input,$out,$key);
		}
		$output.=$out;
	}
	return $output;
}